package cn.com.fssg.platform.base.handler.impl.gsl.lz;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 工商联履职个人填报初始化表单接口
 */
@Component
public class GslLzAssSelectCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String subjectId = jsonObject.getString("assId");
        String zwId = jsonObject.getString("zwId");
        if(StringUtils.isEmpty(zwId)) {
            //如果不传执委ID，则证明是执委查看列表
            zwId = BizUtil.cur().curUserId();
        }
        String querySql = "select * from gsl_audit_assessment_nog_v where subject_id = :subjectId and create_by = :zwId";
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("subjectId", subjectId);
        queryParam.put("zwId", zwId);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(querySql, queryParam);
        if(!list.isEmpty()) {
            Map<String, Object> map = list.get(0);
            //获取图片以及文件信息
            String acId = map.get("id").toString();
            String querySql2 = "select * from wx_file where remark_id = :acId";
            Map<String, Object> queryParam2 = new HashMap<>();
            queryParam2.put("acId", acId);
            List<Map<String, Object>> fileList = jdbcTemplate.queryForList(querySql2, queryParam2);
            List<Map<String, Object>> imgList = new ArrayList<>();
            List<Map<String, Object>> fileLists = new ArrayList<>();
            if(!fileList.isEmpty()) {
                for (Map<String, Object> file : fileList) {
                    String fileType = (String) file.get("type");
                    Map<String, Object> fileMap = new HashMap<>();
                    fileMap.put("id", file.get("id"));
                    fileMap.put("file_name", file.get("display_name"));
                    fileMap.put("form_field", "image".equals(fileType) ? "file_imgs" : "file_Urls");
                    fileMap.put("form_code", "Form20251009104555");
                    fileMap.put("key_value", acId);
                    fileMap.put("file_path", file.get("path"));
                    if("image".equals(fileType)) {
                        imgList.add(fileMap);
                    } else {
                        fileLists.add(fileMap);
                    }
                }
            }
            map.put("file_imgs", imgList);
            map.put("file_Urls", fileLists);
            return map;
        }
        //不存在，则先获取考核表信息
        String assessmentId = jsonObject.getString("assessmentId");
        Map<String, Object> map = new HashMap<>();
        //初始化一个表单ID
        map.put("id", BizUtil.cur().getSnowflakeIdStr());
        map.put("assessment_id", assessmentId);
        map.put("ass_id", subjectId);
        return map;
    }
}
